﻿@page "/signature"

@if (isLoading)
{
    <Loading />
}
else
{
    <div class="signature-box">
        <div class="signature-img">
            <img src="@img_src" />
        </div>
        <div class="signature-action">
            <select @bind="id">
                @foreach (var item in types.Result)
                {
                    <option selected="@(item.Value == id)" value="@item.Value">@item.Description</option>
                }
            </select>
            <input type="text" placeholder="输入你的名字，2-4个字符" maxlength="4" @bind="@name" @bind:event="oninput" />
            <button class="box-btn" @onclick="Generate">免费设计</button>
        </div>
    </div>
    <div class="post-wrap tags">
        <h3>-&nbsp;最近设计的签名&nbsp;-</h3>
        <div class="tag-cloud-tags tag-cloud-tags-extend">
            @foreach (var item in signatures.Result)
            {
                <NavLink @onclick="@(() => ShowSignatureImg(item.Url))">@item.Name</NavLink>
            }
        </div>
    </div>
}

@code {
    /// <summary>
    /// 加载中
    /// </summary>
    private bool isLoading = true;

    /// <summary>
    /// 存放签名图片地址前缀
    /// </summary>
    private string domain = "https://static.meowv.com/signature/";

    /// <summary>
    /// 默认图片
    /// </summary>
    private string img_src = "https://static.meowv.com/images/wxcode.jpg";

    /// <summary>
    /// 默认选中签名类型Id
    /// </summary>
    private int id = 901;

    /// <summary>
    /// 名字
    /// </summary>
    private string name = string.Empty;

    /// <summary>
    /// 签名类型
    /// </summary>
    private ServiceResult<IEnumerable<EnumResponse>> types;

    /// <summary>
    /// 调用记录
    /// </summary>
    private ServiceResult<IEnumerable<SignatureDto>> signatures;

    /// <summary>
    /// 初始化
    /// </summary>
    /// <returns></returns>
    protected override async Task OnInitializedAsync()
    {
        await Common.SetTitleAsync("个性艺术签名设计");

        types = await Http.GetFromJsonAsync<ServiceResult<IEnumerable<EnumResponse>>>($"/signature/types");
        signatures = await Http.GetFromJsonAsync<ServiceResult<IEnumerable<SignatureDto>>>($"/signatures?count=20");

        isLoading = false;
    }

    /// <summary>
    /// 生成签名
    /// </summary>
    private async Task Generate()
    {
        if (string.IsNullOrEmpty(name) || name.Length == 1 || name.Length > 4) return;

        var time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

        var input = new GenerateSignatureInput
        {
            Id = id,
            Name = name,
            Timestamp = time,
            Sign = Common.ToMd5($"{name}_{id}_{time}")
        };

        isLoading = true;

        var response = await Http.PostAsJsonAsync("/signature", input);

        var service = await response.Content.ReadFromJsonAsync<ServiceResult<string>>();
        if (service.Success)
            ShowSignatureImg(service.Result);
        else
            await Common.InvokeAsync("alert", service.Message);

        isLoading = false;
    }

    /// <summary>
    /// 展示签名图片
    /// </summary>
    /// <param name="url"></param>
    private void ShowSignatureImg(string url)
    {
        if (img_src.Contains(url)) return;
        img_src = domain + url;
    }
}